import { Meeting } from '@/api/manager/meeting'
import { Mt } from '@/api/manager/mt'
import { IMcc } from '@/api/manager/type'
import { compareMtKey } from '../../../../utils/utils'
import { MccModel } from './translator'
/** 除vip外 排序 权重 */
export function getWeightExceptVip(mcc: IMcc) {
	let weight = 0
	if (mcc.status.isDoubleFlow) {
		weight += 1000
	}
	if (mcc.status.isSpeaker) {
		weight += 100
	}
	if (mcc.isMt && mcc.status.isChairman) {
		weight += 10
	}
	if (mcc.status.isOnline) {
		weight += 1
	}
	return weight
}
export const meetingListSort = (meetingList: MccModel<Meeting>[], sortType: number) => {
	let result: MccModel<Meeting>[] = []
	if (sortType === 1) {
		result = meetingList.sort((a, b) => a.mcc.index - b.mcc.index)
	} else if (sortType === 2) {
		result = meetingList.sort(
			(a, b) =>
				b.attr.weightExceptVipInSortType2 - a.attr.weightExceptVipInSortType2 || compareMtKey(a.mtKey, b.mtKey)
		)
	} else if (sortType === 3) {
		result = meetingList
	}
	return result
}
export const mtListSort = (mtList: MccModel<Mt>[], sortType: number, vipMtKeys: string[]) => {
	let result: MccModel<Mt>[] = []
	//mtList里的 vip 和 vip列表的vip 不一定一致，以mtList里的为主，vip列表为辅
	const vipInMtList = mtList.filter((x) => x.mcc.status.isVip)
	let _arr: MccModel<Mt>[] = Array(vipMtKeys.length)
	for (const vip of vipInMtList) {
		const index = vipMtKeys.findIndex((mtKey) => mtKey === vip.mtKey)
		if (index !== -1) {
			_arr[index] = vip
		} else {
			_arr.push(vip)
		}
	}
	result = _arr.filter(Boolean)
	const exceptVipInMtList = mtList.filter((x) => !x.mcc.status.isVip)
	if (sortType === 1) {
		result = [...result, ...exceptVipInMtList.sort((a, b) => a.mcc.index - b.mcc.index)]
	} else if (sortType === 2) {
		result = [
			...result,
			...exceptVipInMtList.sort(
				(a, b) =>
					b.attr.weightExceptVipInSortType2 - a.attr.weightExceptVipInSortType2 ||
					compareMtKey(a.mtKey, b.mtKey)
			),
		]
	} else if (sortType === 3) {
		result = mtList
	}
	return result
}
/*
将树结构转成list数组结构
*/
export const flatAllTreeToList = (tree: MccModel<Meeting>) => {
	let stack: MccModel<IMcc>[] = [tree]
	let result = []
	while (stack.length !== 0) {
		const item = stack.shift()!
		if (item.status.isMatchSearch) {
			result.push({
				mcc: item.mcc,
				status: item.status,
				nestingLevel: item.nestingLevel,
				mtKey: item.mtKey,
			})
			if (item.status.isOpen) {
				for (let i = item.children.length - 1; i >= 0; i--) {
					let o = item.children[i]
					stack.unshift({
						mcc: o.mcc,
						status: o.status,
						mtKey: o.mtKey,
						children: o.children,
						nestingLevel: item.nestingLevel + 1,
					} as MccModel<IMcc>)
				}
			}
		}
	}
	return result
}
export const getCurMeetingList = (tree: MccModel<Meeting>) => {
	let stack = tree.children
	let result = []
	for (let i = 0; i < stack.length; i++) {
		const item = stack[i]
		if (item.status.isMatchSearch) {
			result.push({
				mcc: item.mcc,
				status: item.status,
				nestingLevel: 0,
				mtKey: item.mtKey,
			})
		}
	}
	return result
}
/*
遍历tree，并执行 callback(item)

函数使用：
getItemInTree(tree, (item) => {
	......
})
*/
export function getItemInTree(tree: MccModel<Meeting>, callback: (item: MccModel<IMcc>) => void) {
	let stack: MccModel<IMcc>[] = [tree]
	while (stack.length > 0) {
		const item = stack.shift()!
		callback(item)
		if (item.children.length > 0) {
			for (let i = item.children.length - 1; i >= 0; i--) {
				stack.unshift(item.children[i])
			}
		}
	}
}
/*
遍历tree，过滤item，并执行 callback(item)
过滤条件： item.children.length > 0 && item.status.isOpen

函数使用：
getVisibleItemInTree(tree, (item) => {
	......
})
*/
export function getVisibleItemInTree(tree: MccModel<Meeting>, callback: (item: MccModel<IMcc>) => void) {
	let stack: MccModel<IMcc>[] = [tree]
	while (stack.length > 0) {
		const item = stack.shift()!
		callback(item)
		if (item.children.length > 0 && item.status.isOpen) {
			for (let i = item.children.length - 1; i >= 0; i--) {
				stack.unshift(item.children[i])
			}
		}
	}
}
/*
遍历tree，通过mtKey获得tree-item
过滤条件：
if (item.mtKey === mtKey) {
	return item
}

函数使用：
let meetingInTree = getItemByMtKey(tree, data.speaker.mt_key) as MccModel<Mt> | null
*/
export function getItemByMtKey(tree: MccModel<Meeting>, mtKey: string) {
	let stack: MccModel<IMcc>[] = [tree]
	while (stack.length > 0) {
		const item = stack.shift()!
		if (item.mtKey === mtKey) {
			return item
		}
		if (item.children && item.children.length > 0) {
			for (let i = item.children.length - 1; i >= 0; i--) {
				stack.unshift(item.children[i])
			}
		}
	}
	return null
}
export function isCascadeTree(tree: MccModel<Meeting>) {
	if (tree.mtKey === '0.0.0.0_0') {
		let onlineMeeting = tree.children.filter((x) => !x.mcc.isMt && x.mcc.status.isOnline)
		if (onlineMeeting.length === 0) {
			return false
		}
	}
	if (tree.mtKey === '') return false
	return true
}

/*
遍历tree，增加过滤条件，获得item
过滤条件：
if (!item.mcc.isMt && item.mcc.id === meetingMoid) {
	return item
}

函数使用：
let parent = getItemByMeetingMoid(tree, addMeeting.parent_id)
*/
export function getItemByMeetingMoid(tree: MccModel<Meeting>, meetingMoid: string) {
	let stack: MccModel<IMcc>[] = [tree]
	while (stack.length > 0) {
		const item = stack.shift()!
		if (!item.mcc.isMt && item.mcc.id === meetingMoid) {
			return item
		}
		if (item.children && item.children.length > 0) {
			for (let i = item.children.length - 1; i >= 0; i--) {
				stack.unshift(item.children[i])
			}
		}
	}
	return null
}

/*
遍历tree，通过子节点mtKey增加过滤条件，获得父节点item
过滤条件：
const childrenMtKeys = item.children.map((x) => x.mtKey)
if (childrenMtKeys.includes(mtKey)) {
	return item
}

函数使用：
let parent = getParentByChildMtKey(tree, mtKey)
*/
export function getParentByChildMtKey(tree: MccModel<Meeting>, mtKey: string) {
	let stack: MccModel<IMcc>[] = [tree]
	while (stack.length > 0) {
		const item = stack.shift()!
		if (item.children && item.children.length > 0) {
			/*
			children中的mtKey，存在参数mtKey，就return 父节点item
			*/
			const childrenMtKeys = item.children.map((x) => x.mtKey)
			if (childrenMtKeys.includes(mtKey)) {
				return item
			}
			for (let i = item.children.length - 1; i >= 0; i--) {
				stack.unshift(item.children[i])
			}
		}
	}
	return null
}
/*
遍历tree，增加过滤条件，获得item
过滤条件：
const childrenMeetingMoids = (item.children.filter((x) => !x.mcc.isMt) as MccModel<Meeting>[]).map(
	(x) => x.mcc.id
)
if (childrenMeetingMoids.includes(meetingMoid)) {
	return item
}

函数使用：
let parent = getParentByChildMeetingMoid(tree, id)
*/
export function getParentByChildMeetingMoid(tree: MccModel<Meeting>, meetingMoid: string) {
	let stack: MccModel<IMcc>[] = [tree]
	while (stack.length > 0) {
		const item = stack.shift()!
		if (item.children && item.children.length > 0) {
			/*
			children中的id集合中，存在参数 meetingMoid，返回父节点item

			.map返回id的数组集合
			*/
			const childrenMeetingMoids = (item.children.filter((x) => !x.mcc.isMt) as MccModel<Meeting>[]).map(
				(x) => x.mcc.id
			)
			/*
			includes 数组中是否存在某个元素
			*/
			if (childrenMeetingMoids.includes(meetingMoid)) {
				return item
			}
			for (let i = item.children.length - 1; i >= 0; i--) {
				stack.unshift(item.children[i])
			}
		}
	}
	return null
}
